﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using Web.Views.DataManage.Models;
using ePlanet.SingleWell.Models;
using ePlanet.SingleWell.DataAccess;

namespace Web.Views.DataManage
{
    /// <summary>
    /// SingleWellDataUploadHandler 的摘要说明
    /// </summary>
    public class SingleWellDataUploadHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {            
            JavaScriptSerializer jss = new JavaScriptSerializer();
            try
            {
                context.Response.ContentType = "text/plain";
                byte[] tempData = new byte[context.Request.InputStream.Length];
                context.Request.InputStream.Read(tempData, 0, tempData.Length);
                System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding();
                string jsonStr = utf8.GetString(tempData);
                if (!string.IsNullOrEmpty(jsonStr))
                {
                     //SingleWellDataUpdateModel model = jss.Deserialize<SingleWellDataUpdateModel>(jsonStr);
                     //foreach (SingleWellViewModel singleWellViewModel in model.InsertedRows)
                     //{
                     //    SingleWellModel sModel = new SingleWellModel();
                     //    SingleWellModel.SINGLEWELLRow row = sModel.SINGLEWELL.NewSINGLEWELLRow();
                     //    row.WELLDATAID = singleWellViewModel.WellDataID;
                     //    row.WELLCODE = singleWellViewModel.WellCode;
                     //    row.DATAFILENAME = singleWellViewModel.DataFileName;
                     //    row.WORKMODE = singleWellViewModel.WorkMode;
                     //    row.A4 = singleWellViewModel.A4;
                     //    row.A3 = singleWellViewModel.A3;
                     //    row.A0 = singleWellViewModel.A0;
                     //    row.BEGINDATE = singleWellViewModel.BeginDate;
                     //    row.FINISHDATE = singleWellViewModel.FinishDate;
                     //    row.WELLTYPE = singleWellViewModel.WellType;
                     //    row.BEGINWORKDATE = singleWellViewModel.BeginWorkDate;
                     //    row.FINISHWORKDATE = singleWellViewModel.FinishWorkDate;
                     //    row.WELLDEPTH = singleWellViewModel.WellDepth;
                     //    row.ARCHIVECODE = singleWellViewModel.ArchiveCode;
                     //    row.QUANZONGCODE = singleWellViewModel.QuanZongCode;
                     //    sModel.SINGLEWELL.AddSINGLEWELLRow(row);
                     //    SingleWellDAl dal = new SingleWellDAl();
                     //    dal.AddNewSingleWellModel(sModel);
                     //}
                     //foreach (SingleWellViewModel singleWellViewModel in model.InsertedRows)
                     //{
                     //    SingleWellModel sModel = new SingleWellModel();
                     //    SingleWellModel.SINGLEWELLRow row = sModel.SINGLEWELL.NewSINGLEWELLRow();
                     //    row.WELLDATAID = singleWellViewModel.WellDataID;
                     //    row.WELLCODE = singleWellViewModel.WellCode;
                     //    row.DATAFILENAME = singleWellViewModel.DataFileName;
                     //    row.WORKMODE = singleWellViewModel.WorkMode;
                     //    row.A4 = singleWellViewModel.A4;
                     //    row.A3 = singleWellViewModel.A3;
                     //    row.A0 = singleWellViewModel.A0;
                     //    row.BEGINDATE = singleWellViewModel.BeginDate;
                     //    row.FINISHDATE = singleWellViewModel.FinishDate;
                     //    row.WELLTYPE = singleWellViewModel.WellType;
                     //    row.BEGINWORKDATE = singleWellViewModel.BeginWorkDate;
                     //    row.FINISHWORKDATE = singleWellViewModel.FinishWorkDate;
                     //    row.WELLDEPTH = singleWellViewModel.WellDepth;
                     //    row.ARCHIVECODE = singleWellViewModel.ArchiveCode;
                     //    row.QUANZONGCODE = singleWellViewModel.QuanZongCode;
                     //    sModel.SINGLEWELL.AddSINGLEWELLRow(row);
                     //    SingleWellDAl dal = new SingleWellDAl();
                     //    //dal.AddNewSingleWellModel(sModel);
                     //}
                     //foreach (SingleWellViewModel singleWellViewModel in model.InsertedRows)
                     //{
                     //    SingleWellModel sModel = new SingleWellModel();
                     //    SingleWellModel.SINGLEWELLRow row = sModel.SINGLEWELL.NewSINGLEWELLRow();
                     //    row.WELLDATAID = singleWellViewModel.WellDataID;
                     //    sModel.SINGLEWELL.AddSINGLEWELLRow(row);
                     //    SingleWellDAl dal = new SingleWellDAl();
                     //    //dal.AddNewSingleWellModel(sModel);
                     //}
                     context.Response.Write(jss.Serialize(new {responseCode = "Update Success",response = ""}));
                     context.Response.End();
                }
                context.Response.Write(jss.Serialize(new { responseCode = "Update None", response = "" }));
                context.Response.End();
            }
            catch (Exception ex)
            {
                context.Response.Write(jss.Serialize(new { responseCode = "Server Error",responseText=ex.Message }));
                context.Response.End();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}